package com.yxw.live_vod_backend_boot.controller;

import com.yxw.live_vod_backend_boot.dto.*;
import com.yxw.live_vod_backend_boot.entity.YxCsUserMenuEntity;
import com.yxw.live_vod_backend_boot.service.IUserService;
import com.yxw.live_vod_backend_boot.vo.UserEmpVO;
import com.yxw.live_vod_backend_boot.vo.YxCsMenuVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.helper.ResultHelper;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理Controller控制层处理
 *
 * @author xiexuxin
 * @date 2023-09-19
 */
@Slf4j
@Api(value = "员工管理", tags = "员工管理")
@RestController
@Validated
@RequestMapping("/v1/emp")
public class UserEmpController {

    @Autowired
    private IUserService empService;

    /**
     * 新增员工
     */
    @ApiOperation("添加员工")
    @PostMapping("add_emp")
    public BaseResult add(@RequestBody UserEmpDTO empDTO) {
        log.info("开始处理【添加员工】的请求，参数：{}", empDTO);
        empService.addUser(empDTO);
        return BaseResult.success();
    }

    /**
     * 删除员工
     */
    @ApiOperation("删除员工")
    @PostMapping("/batch_delete")
    public BaseResult batchDelete(@RequestBody EmpDeleteDTO dto) {
        log.info("开始处理【根据id批量删除员工】的请求，参数：{}", dto);
        empService.deleteUserByIds(dto);
        return BaseResult.success();
    }

    /**
     * 修改员工
     */
    @ApiOperation("根据id修改员工")
    @PostMapping("/update")
    public BaseResult edit(@RequestBody UserEmpUpdateDTO empDTO) {
        log.info("开始处理【根据id修改员工】的请求,新数据：{}",empDTO);
        empService.updateUser(empDTO);
        return BaseResult.success();
    }

    /**
     * 修改员工头像
     */
    @ApiOperation("根据id修改员工头像")
    @PostMapping("/update_pic_url")
    public BaseResult editPicUrl( @RequestParam("id") Long id, @RequestParam String picUrl) {
        log.info("开始处理【根据id修改员工头像】的请求，ID：{}，头像路径：{}", id,picUrl);
        empService.updateUserPicUrl(id,picUrl);
        return BaseResult.success();
    }

    /**
     * 获取员工详细信息
     */
    @ApiOperation(value = "根据id查询员工信息")
    @GetMapping(value = "/select_emp")
    public BaseResult getInfo(@RequestParam("id") Long id) {
        log.info("开始处理【根据id查询员工信息】的请求，ID：{}", id);
        UserEmpVO empVO = empService.selectUserById(id);
        return BaseResult.success(empVO);
    }

    /**
     * 查询员工列表
     */
    @ApiOperation(value = "查询员工列表")
    @PostMapping("/user_list")
    public BaseResult userList() {
        log.info("开始处理【查询员工列表】请求，无参数");
        List<UserEmpVO> list = empService.userList();
        return BaseResult.success(list);
    }
    
    /**
     *条件查询员工列表
     */
    @ApiOperation(value = "条件查询员工")
    @PostMapping(value = "/get_user_list")
    public BaseResult<PageVO<UserEmpVO>> getUserList(@RequestBody UserEmpQueryDTO empQueryDTO) {
        log.info("开始处理【条件查询员工】请求，参数:{}",empQueryDTO);
        PageVO<UserEmpVO> pageVo=empService.getUserList(empQueryDTO);
        return ResultHelper.fillWithSuccess(pageVo);
    }

    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/change_password")
    public BaseResult changePassword(@RequestBody PasswordDTO passwordDTO) {
        log.info("开始处理【修改密码】请求，参数:{}", passwordDTO);
        val aBoolean = empService.changePassword(passwordDTO);
        return BaseResult.success(aBoolean);

    }

    /**
     * 重置密码
     */
    @ApiOperation(value = "重置密码")
    @PostMapping(value = "/reset_passwords")
    public BaseResult resetPasswords(@RequestBody ResetPasswordsDTO dto) {
        log.info("开始处理【重置密码】请求，参数:{}", dto);
        val aBoolean = empService.resetPasswords(dto);
        return BaseResult.success(aBoolean);

    }

    /**
     * 员工编辑权限
     */
    @ApiOperation(value = "员工编辑权限")
    @PostMapping("/edit_user")
    public BaseResult editUserPermissions(@RequestBody List<YxCsUserMenuEntity> userMenus) {
        empService.updateUserMenuByUserId(userMenus);
        return BaseResult.success();
    }

    /**
     * 加载对应员工菜单列表树
     */
    @PostMapping("/get_menu_tree_by_user")
    @ApiOperation(value = "加载对应员工菜单列表树")
    public BaseResult getMenuTreeByUser(@RequestParam("userId") Long userId) {
        List<YxCsMenuVo> menuList = empService.getMenuTreeByUser(userId);
        return BaseResult.success(menuList);
    }

    /**
     * 根据用户id查询角色返回菜单树
     */
    @PostMapping("/get_menu_tree_by_user_menu")
    @ApiOperation(value = "根据用户id查询角色返回菜单树")
    public BaseResult getMenuTreeByUserMenu(@RequestParam("userId") Long userId) {
        List<YxCsMenuVo> menuList = empService.getMenuTreeByUserMenu(userId);
        return BaseResult.success(menuList);
    }
}
